package com.sgb.kjwl.view.ui.adapter.manage;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class ManageMainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<String> mVideo;
    List<String> mInfo;
    Context context;
    private static final int VIEW_TYPE_INFO = 0;
    private static final int VIEW_TYPE_VIDEO = 1;
    private static final int VIEW_TYPE_VIDEO_ONE = 2;
    private int mNum = 0;

    public ManageMainAdapter(List<String> mVideo, List<String> mInfo, Context context) {
        this.mVideo = mVideo;
        this.mInfo = mInfo;
        this.context = context;
    }

    @NonNull
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        switch (viewType){
            case VIEW_TYPE_INFO:
                return new InfoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_ecommended_enterprises,parent,false));
            case VIEW_TYPE_VIDEO:
                return new VideoViewHolder(LayoutInflater.from(context).inflate(R.layout.item_video_list,parent,false));
            case VIEW_TYPE_VIDEO_ONE:
                return new OneVideoHolder(LayoutInflater.from(context).inflate(R.layout.item_one_video,parent,false));
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()){
            case VIEW_TYPE_INFO:
                InfoViewHolder infoViewHolder = (InfoViewHolder) holder;
                Glide.with(context).load(mInfo.get(position)).apply(new RequestOptions().placeholder(R.mipmap.gongsiqiye).fallback( R.mipmap.gongsiqiye).error(R.mipmap.gongsiqiye)).into(infoViewHolder.mLogoView);
                break;
            case VIEW_TYPE_VIDEO:
                VideoViewHolder videoViewHolder = (VideoViewHolder) holder;
                break;
            case VIEW_TYPE_VIDEO_ONE:
                OneVideoHolder oneVideoHolder = (OneVideoHolder) holder;
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mInfo.size() + mNum;
    }

    @Override
    public int getItemViewType(int position) {
        if (mInfo.size()<10){
            if (position < mInfo.size()){
                return VIEW_TYPE_INFO;
            }else {
                mNum++;
                if (mVideo.size() > 1){
                    return VIEW_TYPE_VIDEO;
                }else {
                    return VIEW_TYPE_VIDEO_ONE;
                }
            }
        }else {
            if (position % 11 != 0){
                return VIEW_TYPE_INFO;
            }else{
                mNum++;
                if (mVideo.size() > 1){
                    return VIEW_TYPE_VIDEO;
                }else {
                    return VIEW_TYPE_VIDEO_ONE;
                }
            }
        }
    }

    class InfoViewHolder extends RecyclerView.ViewHolder {

        ImageView mLogoView;
        TextView mTvItemCommendedEnterpriseTitle,mTvFollow,mServiceType,mTvCount,mTvMainBusiness,mTvAddress,mTvDistance;
        public InfoViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            mLogoView = itemView.findViewById(R.id.logo_view);
            mTvItemCommendedEnterpriseTitle = itemView.findViewById(R.id.tv_item_commended_enterprise_title);
            mTvFollow = itemView.findViewById(R.id.tv_follow);
            mServiceType = itemView.findViewById(R.id.tv_service_type);
            mTvCount = itemView.findViewById(R.id.tv_count);
            mTvMainBusiness = itemView.findViewById(R.id.tv_mainBusiness);
            mTvAddress = itemView.findViewById(R.id.tv_address);
            mTvDistance = itemView.findViewById(R.id.tv_distance);
        }
    }

    class VideoViewHolder extends RecyclerView.ViewHolder{

        public VideoViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
        }
    }

    class OneVideoHolder extends RecyclerView.ViewHolder{

        public OneVideoHolder(@NonNull @NotNull View itemView) {
            super(itemView);
        }
    }
}
